﻿using M.WWHours.API.Command.WWHoursCommand;
using M.WWHours.Repository.WWHoursImpl;
using M.WWHours.Repository.WWHoursInterface;
using MediatR;

namespace M.WWHours.API.CommandHandlers.WWHoursCommandHandler
{
    /// <summary>
    /// 删除工时命令处理器
    /// </summary>
    public class DeletedWWHoursCommandHandler : IRequestHandler<DeletedWWHoursCommand, int>
    {
        /// <summary>
        /// 工时工时仓储接口
        /// </summary>
        protected readonly IWWHoursRepository wWHoursRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="wWHoursRepository">工时工时仓储接口</param>
        public DeletedWWHoursCommandHandler(IWWHoursRepository wWHoursRepository)
        {
            this.wWHoursRepository = wWHoursRepository;
        }

        /// <summary>
        /// 处理删除工时命令
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentException"></exception>
        public async Task<int> Handle(DeletedWWHoursCommand request, CancellationToken cancellationToken)
        {
            var result = await wWHoursRepository.GetModelsAsync(request.WWHoursId);
            if (result != null)
            {
                result.IsDeleted = true;
                result.DeletedBy = request.DeletedBy;
                result.DeletedDate = DateTime.Now;
            }
            else
            {
                throw new ArgumentException("该工人工时信息不存在");
            }
            return await wWHoursRepository.UpdateAsync(result);
        }
    }
}
